# 利用tailhead软件包跟踪文件变化
import tailhead
# 字符串解析
import parse
# 时间包
import datetime
import os
import time

# 调用客户端
from thingsboard_gateway.tb_client.tb_device_mqtt import TBDeviceMqttClient

# 用于自动生成命令行
import fire

# 输出log，便于调试
import logging

def main(host='test2.bj-jiashi.com',
        port=10006,
        token='POPS', 
        delay_time=0.5, 
        target_log_file='sub3_POPS_simulator.py.test.log',
        log_level = 'INFO'):
    # 初始化
    # 初始化
    logging.basicConfig(level = getattr(logging,log_level.upper(),logging.INFO)) # 默认info
    file_base_name = os.path.basename(__file__)
    logger = logging.getLogger(file_base_name)

    # 分隔符
    sep = ','
    # 列名
    col_names = 'DateTime,TimeSSM,Status,DataStatus,PartCt,HistSum,PartCon,BL,BLTH,STD,MaxSTD,P,TofP, PumpLife_hrs,WidthSTD,AveWidth, POPS_Flow, PumpFB, LDTemp, LaserFB, LD_Mon, Temp, BatV, Laser_Current, Flow_Set,BL_Start,TH_Mult,nbins,logmin,logmax,Skip_Save,MinPeakPts,MaxPeakPts,RawPts,b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15'.split(sep)
    col_names = [n.strip() for n in col_names]
    # 格式
    # example 
    # 1610010201.664,32601.6641,3,2,6947,6947,5680.02,2125,2149,8.06,13.72,1033.35,25.74,105.64,0.00,0.00,1.22,69.45,36.50,621.54,757.80,26.15,11.24,2.87,1.90,30000,3.0,16,1.00,4.81,0,8,255,512,0,7,2385,3036,402,312,289,241,222,38,12,2,1,0,0,0
    write_formats = ['14.3f','.4f']+['d']*4+['.2f','d','d']+['.2f']*16+['d','f','d','.2f','.2f']+['d']*20
    read_formats = [format[-1] for format in write_formats]

    # data types
    data_types = dict(zip(col_names, read_formats))

    # selected cols
    selected_cols = [col.strip() for col in 'PartCon,b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15'.split(',') ]
    
    # 默认值，为保持完整性
    defaults_str = '1610010201.664,32601.6641,3,2,6947,6947,5680.02,2125,2149,8.06,13.72,1033.35,25.74,105.64,0.00,0.00,1.22,69.45,36.50,621.54,757.80,26.15,11.24,2.87,1.90,30000,3.0,16,1.00,4.81,0,8,255,512,0,7,2385,3036,402,312,289,241,222,38,12,2,1,0,0,0'

    # 第一行
    header = sep.join(col_names)
    logger.info(header)

    # 生成格式化字符串
    line_write_formatter=sep.join([f'{{{col_name}:{format}}}' for col_name,format in zip(col_names,write_formats)])
    logger.info(line_write_formatter)

    line_read_formatter=sep.join([f'{{{col_name}:{format}}}' for col_name,format in zip(col_names,read_formats)])
    logger.info(line_read_formatter)

    # 简单测试
    parse_result = parse.parse(line_read_formatter,defaults_str)
    logger.info(parse_result)
    if parse_result is None:
        raise RuntimeError('Something go wrong!')

    # 默认值
    data_dict_defaults = parse_result.named

    # example data
    # raw_data_example_filename = r"raw_data\课题3\POPS\POPS_20210107x003.csv"
    
    # 建立设备连接
    TBD = TBDeviceMqttClient(host, port, token)
    TBD.connect()
    logger.info('mqtt server connected!')

    for line in tailhead.follow_path(target_log_file,encoding='utf-8'):
        if line is not None:
            logger.debug(f"readline: {line}")
            # 解析
            parse_result = parse.parse(line_read_formatter, line)
            if parse_result is None:
                logger.debug('not a valid data line')
                continue 
            data_line_dict = parse_result.named

            data_dict = {'ts':-1,'values':{}}
            # 时间解析
            # 假设是UTC时间，如果是北京时间需要调整
            data_dict['ts'] = int(data_line_dict['DateTime']*1000) # 转换为ms数

            # 数据抽取
            data_dict['values'] = {col:data_line_dict.get(col,-99999) for col in selected_cols}

            logger.info(f'data_dict={data_dict}')

            # 上传
            TBD.send_telemetry(data_dict)
            time.sleep(delay_time)
        else:
            logger.info('sleep.')
            time.sleep(delay_time)

if __name__ == "__main__":
    fire.Fire(main)
    # e.g
    # python demo_tail_log_upload.py --DELAY 0.5 --LOG_FILE your_file.log